/**
 
 * 
 */
package nl.boektrust.btsoep.server;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nl.boektrust.btsoep.shared.model.BTEntity;
import nl.boektrust.btsoep.shared.model.Book;
import nl.boektrust.btsoep.shared.model.BookManifestation;
import nl.boektrust.btsoep.shared.model.Person;
import nl.boektrust.btsoep.shared.model.PhysicalBookManifestation;

import com.googlecode.objectify.ObjectifyService;

/**
 * @author rao3
 *
 * the implementation of the server that can serve XML requests to Koppelvlak bookstores. 
 */
public class ChunkTask extends HttpServlet {
	private boolean isObjectifyServiceRegistered = false;
	private ServletContext context;

	
	ServletContext getContext() {
		return this.context;
	}

	public void init() throws ServletException {
		context = getServletContext();
		//System.err.println("Real Path = " + context.getRealPath("/"));
	}

	/**
	 * 
	 */
	public ChunkTask() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param delegate
	 */
	public ChunkTask(Object delegate) {
		//super(delegate);
		// TODO Auto-generated constructor stub
	}
	
	private void registerServices () {
		if (isObjectifyServiceRegistered == false) {

			ObjectifyService.register(BTEntity.class);
			ObjectifyService.register(Person.class);
			ObjectifyService.register(PhysicalBookManifestation.class);
			ObjectifyService.register(BookManifestation.class);
			ObjectifyService.register(Book.class);
			isObjectifyServiceRegistered = true;
		}
	}
	
	public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws ServletException, IOException  
	{
		
		/*
		 * get raw XML from the inputstream. 
		 */
		System.err.println("task chunkTask ");
		System.err.println("paramter key is "+request.getParameter("key"));
		String parameter = request.getParameter("key");
		int chunk = Integer.parseInt(parameter);;
		System.err.println("task chunkTask key is "+chunk);
		new PersistentServiceImpl().updatePhysicalBookManifestationChunk(chunk);
		System.err.println("task chunkTask finished");

	} 
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {

		System.err.println("task chunkTask ");
		int chunk = Integer.parseInt((String)request.getAttribute("key"));
		new PersistentServiceImpl().updatePhysicalBookManifestationChunk(chunk);
		
		System.err.println("task chunkTask finished");
		
	}


}
